<template>
  <div id="goods">
    <!-- 面包屑区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>商品管理</el-breadcrumb-item>
      <el-breadcrumb-item>商品列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片视图区域 -->
    <el-card>
      <el-row :gutter="20">
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="QueryInfo.query" clearable @clear='getGoodListData'>
            <el-button slot="append" icon="el-icon-search" @click="getGoodListData"></el-button>
          </el-input>
        </el-col>
        <el-col :span='4'>
          <el-button type="primary" @click="goAddPage">添加列表</el-button>
        </el-col>
      </el-row>
      <!-- table表格区域 -->
      <el-table :data="goodListData" border stripe>
        <el-table-column type='index' label="#"></el-table-column>
        <el-table-column prop='goods_name' label="商品名称"></el-table-column>
        <el-table-column prop='goods_price' label="商品价格" width="90px"></el-table-column>
        <el-table-column prop='goods_weight' label="商品重量" width="75px"></el-table-column>
        <el-table-column prop='add_time' label='创建时间' width="180px">
          <template slot-scope="scope">
            {{scope.row.add_time | filterDate}}
          </template>
        </el-table-column>
        <el-table-column label='操作' width="135px">
          <template>
            <el-button type="primary" icon="el-icon-edit" size="mini"></el-button>
            <el-button type="danger" icon="el-icon-delete" size="mini"></el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页功能 -->
      <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="QueryInfo.pagenum"
      :page-sizes="[1, 2, 5, 10, 20]"
      :page-size="QueryInfo.pagesize"
      layout="total, sizes, prev, pager, next, jumper"
      :total="total" background
      >
      </el-pagination>
    </el-card>
  </div>
</template>

<script>

export default {
  name: 'goods',
  data () {
    return {
      // 商品列表请求参数
      QueryInfo: {
        query: '',
        // 当前页码
        pagenum: 1,
        // 每页显示条数
        pagesize: 10
      },
      // 商品列表数据
      goodListData: [],
      // 商品总数
      total: 0
    }
  },
  components: {
  },
  methods: {
    // 获取我的商品列表数据
    async getGoodListData () {
      const { data: res } = await this.$http.get('goods', {
        params: this.QueryInfo
      })
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      // this.$message.success(res.meta.msg)
      this.goodListData = res.data.goods
      this.total = res.data.total
      // console.log(this.total)
      // console.log(this.goodListData)
    },
    // 分页符每页显示改变触发
    handleSizeChange (val) {
      this.QueryInfo.pagesize = val
      this.getGoodListData()
    },
    // 分页符页数改变触发
    handleCurrentChange (val) {
      this.QueryInfo.pagenum = val
      this.getGoodListData()
    },
    // 点击跳转到 添加页面
    goAddPage () {
      this.$router.push('/goodadd')
    }
  },
  created () {
    this.getGoodListData()
  },
  computed: {}
}
</script>

<style>
 .el-table {
   margin-top: 25px;
 }
</style>
